//
//  PBTabPageViewController.mm
//  PlatinumButler
//
//  Created by 高小松 on 14-3-10.
//  Copyright (c) 2014年 gxsoft. All rights reserved.
//

#import "PBTabPageViewController.h"
#import "PBUIConfig.h"
#import "PBNaviTitleView.h"
#import "PBCollectionViewCell.h"

// 页面上方标题栏默认高度
#define DEFAULT_NAVI_TITLE_HEIGHT 40

// iOS 6和iOS 7上方需要空出来的状态栏高度
#define IOS6_STATUS_BAR_BLANK 0
#define IOS7_STATUS_BAR_BLANK 20

@implementation PBTabPageViewController

#pragma mark - Memory Management

- (id)init
{
    self = [super init];
    if (self) {
        // 标签页项
        self.tabBarItem = [[[UITabBarItem alloc] init] autorelease];
        [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[[PBUIConfig instance] tabBarTextColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
        [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[[PBUIConfig instance] tabBarTextHighlightedColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];
    }
    return self;
}

- (void)dealloc
{
    [_naviTitle release];
    [_collectionView release];
    [_dataSource release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View Management

- (void)loadView
{
    CGRect rect = [[UIScreen mainScreen] bounds];
    UIView * v = [[[UIView alloc] initWithFrame:rect] autorelease];
    v.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    v.backgroundColor = [UIColor clearColor];
    self.view = v;
    // 遮挡
    UIColor * backgroundColor = [[PBUIConfig instance] naviTitleBackgroundColor];
    CGRect rcBlankView = CGRectMake(0, 0, CGRectGetWidth(rect), [self statusBarBlank]);
    UIView * blankView = [[[UIView alloc] init] autorelease];
    blankView.frame = rcBlankView;
    blankView.backgroundColor = backgroundColor;
    blankView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
    [self.view addSubview:blankView];
    // 标题栏
    if (_hasNaviTitle) {
        CGRect rcNaviTitle = CGRectMake(0, [self statusBarBlank], CGRectGetWidth(rect), DEFAULT_NAVI_TITLE_HEIGHT);
        UIColor * textColor = [[PBUIConfig instance] naviTitleTextColor];
        _naviTitle = [[PBNaviTitleView alloc] init];
        _naviTitle.frame = rcNaviTitle;
        _naviTitle.backgroundColor = backgroundColor;
        _naviTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
        [_naviTitle setTextColor:textColor];
        [self.view addSubview:_naviTitle];
    }
    // 列表布局
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.itemSize = CGSizeMake(60, 80);
    _flowLayout.minimumLineSpacing = 10;
    _flowLayout.minimumInteritemSpacing = 15;
    _flowLayout.sectionInset = UIEdgeInsetsMake(15, 10, 15, 10);
    // 功能列表
    CGRect rcCollectionView = [self contentRect];
    _collectionView = [[UICollectionView alloc] initWithFrame:rcCollectionView collectionViewLayout:_flowLayout];
    _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_collectionView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_naviTitle setTitle:self.title];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[PBCollectionViewCell class] forCellWithReuseIdentifier:[self dequeueReusableCellWithReuseIdentifier]];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_dataSource count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PBCollectionViewCell * cell = (PBCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[self dequeueReusableCellWithReuseIdentifier] forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[[PBCollectionViewCell alloc] init] autorelease];
    }
    NSInteger row = indexPath.row;
    [cell setTitle:[_dataSource objectAtIndex:row]];
    [cell setImage:[UIImage imageNamed:[_dataImage objectAtIndex:row]]];
    return cell;
}

#pragma mark - NaviTitle

- (void)addLeftButton
{
    
}

- (void)addRightButton
{
    
}

#pragma mark - Funtion

- (CGRect)contentRect
{
    CGRect rect = self.view.bounds;
    CGFloat hOffset = [self statusBarBlank];
    if (_hasNaviTitle && _naviTitle != nil) {
        hOffset += CGRectGetHeight(_naviTitle.bounds);
    }
    return CGRectMake(0, hOffset, CGRectGetWidth(rect), CGRectGetHeight(rect) - hOffset);
}

- (NSString *)dequeueReusableCellWithReuseIdentifier
{
    return @"PBTabPageViewController";
}

- (int)statusBarBlank
{
    return ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0 ? IOS6_STATUS_BAR_BLANK : IOS7_STATUS_BAR_BLANK);
}

@end
